package com.admin.socketmultiplayer.dialog

import android.content.Context
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.BmobUser
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.UpdateListener
import com.admin.socketmultiplayer.bean.MyUser
import com.admin.socketmultiplayer.config.DataUtils
import com.admin.socketmultiplayer.config.FixedValue
import com.socks.library.KLog
import kotlinx.android.synthetic.main.dialog_user_info.*
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/5/9.
 */
class UserInfoChangeDialog(context: Context,layout:Int,theme:Int,var listener:OnCancelListener):BaseDialog(context,layout,theme){
    var user:MyUser?=null
    var index=0

    override fun initData() {
        user=BmobUser.getCurrentUser(MyUser::class.java)
        edt_c_name.setText(user!!.username)
        edt_c_phone.setText(user!!.mobilePhoneNumber)
        edt_c_email.setText(user!!.email)
        edt_c_qq.setText(user!!.QQ)
        edt_c_weichat.setText(user!!.weChat)
        initEvent()
    }

    fun initEvent(){
        btn_c_cancel.setOnClickListener { cancel() }
        btn_c_commit.setOnClickListener {
            var my=MyUser()
            var userName=edt_c_name.text.toString()
            if(userName!=""&& userName!=user!!.username){
                my.username=userName
                index++
            }
            var phone=edt_c_phone.text.toString()
            if(phone!=""&&phone!=user!!.mobilePhoneNumber){
                if(!DataUtils.isPhoneNumRight(phone,FixedValue.PHONE_STANDARD)){
                    context.toast("手机号码格式错误")
                    return@setOnClickListener
                }
                my.mobilePhoneNumber=phone
                index++
            }
            var email=edt_c_email.text.toString()

            if(email!=""&&email!=user!!.email){
                if(!DataUtils.isPhoneNumRight(email,FixedValue.EAMIL)){
                    context.toast("邮件格式错误")
                    return@setOnClickListener
                }else{
                    var query=BmobQuery<MyUser>()
                    query.addWhereEqualTo("email",email)
                    query.findObjects(object:FindListener<MyUser>(){
                        override fun done(p0: MutableList<MyUser>?, p1: BmobException?) {
                            if (p0!=null&&p0.size>0&&p0[0].username!=user!!.username){
                                context.toast("该邮箱已被其他用户占用")
                            }else{
                                my.email=email
                                index++
                                updataUser(my)
                            }
                        }
                    })
                }
            }else{updataUser(my)}

        }
    }

    fun updataUser(myUser: MyUser){
        var qq=edt_c_qq.text.toString()
        if(qq!=""&&qq!=user!!.QQ){
            myUser.QQ=qq
            index++
        }
        var weic=edt_c_weichat.text.toString()
        if(weic!=""&&weic!=user!!.weChat){
            myUser.weChat=weic
            index++
        }
        if(index==0){
            cancel()
        }else{
            myUser.update(user!!.objectId,object :UpdateListener(){
                override fun done(p0: BmobException?) {
                    p0?:let { context.toast("更新个人信息成功")
                        listener!!.onCancelListener("r")
                    }
                    p0?.let { context.toast("更新个人信息失败")
                        KLog.e(p0.message.toString())
                        listener!!.onCancelListener("e")
                    }
                    index=0
                    cancel()
                }
            })
        }
    }

    interface OnCancelListener{
        fun onCancelListener(msg:String)
    }

}